package newx.com.control.comboBox
{
	import flash.events.Event;
	import flash.utils.getQualifiedClassName;
	
	import newx.com.control.list.NxListEvent;
	
	/**
	 * 
	 * @author gf
	 * 
	 */	
	
	public class NxComboBoxSelect extends NxComboBox
	{
		
		public function NxComboBoxSelect()
		{
			super();
		}
		
		override protected function addListener():void
		{
			super.addListener();
			_titleItem.addEventListener(LabelItemEvent.TEXT_INPUT,searchItem);
			_nxList.addEventListener(NxListEvent.ITEM_ROLLOVER,sceneListOver);
			_nxList.addEventListener(NxListEvent.ITEM_ROLLOUT,sceneListOut);
			_nxList.addEventListener(NxListEvent.LIST_CLICK,sceneListClick);
			
		}
		
		override protected function showOrHideList(e:Event):void
		{
			var clickTarget:String = getQualifiedClassName(e.target);
			
			if(clickTarget.split("::")[1] == "TextField")
			{
				searchItem();
			}
			else
			{
				_nxList.dataProvider = _dataList;
				showList();
			}
		}
		
		private function sceneListClick(e:NxListEvent):void
		{
			dispatchEvent(new NxComboBoxSelectEvent(NxComboBoxSelectEvent.NXCOMBOBOXSELECT_CLICK,e.itemData));
		}
		
		
		private function sceneListOver(e:NxListEvent):void
		{
			dispatchEvent(new NxComboBoxSelectEvent(NxComboBoxSelectEvent.NXCOMBOBOXSELECT_OVER,e.itemData));
		}
		
		private function sceneListOut(e:NxListEvent):void
		{
			dispatchEvent(new NxComboBoxSelectEvent(NxComboBoxSelectEvent.NXCOMBOBOXSELECT_OUT,e.itemData));
		}
		
		private function searchItem(e:LabelItemEvent = null):void
		{
			if(editTitle != "")
			{
				var searchArr:Array = getFindItems(_dataList,editTitle);
				_nxList.dataProvider = searchArr;
			}
			else
			{
				_nxList.dataProvider = _dataList;
			}
			
			showList();
		}
		
		private function getFindItems(source:Array,findStr:String):Array
		{
			var returnArr:Array	=	[];
			source.forEach(function(item:*,index:int,array:Array):void{
				if(item.label.indexOf(findStr) != -1) returnArr.push(item);
			},this);
			return returnArr;
		}
		
		/**
		 * 文本是否可以编辑（模糊查询）
		 * @param boolean
		 * 
		 */		
		
		public function set edited(boolean:Boolean):void
		{
			_titleItem.txtLabelInput = boolean;
		}
		
		public function get editTitle():String
		{
			return _titleItem.txtTitle;
		}
		
		public function set editTitle(val:String):void
		{
			_titleItem.txtTitle = val;
		}
	}
}